package com.pomr.controller.simstorage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pomr.model.equipmentstorage.EquipmentStorage;
import com.pomr.model.equipmentstorage.EquipmentStorageDetail;
import com.pomr.model.menu.EquipmentKind;
import com.pomr.model.simstorage.SimStorage;
import com.pomr.model.simstorage.SimStorageDetail;
import com.pomr.model.synthesize.Manufacturer;
import com.pomr.model.sys.User;
import com.pomr.service.simstorage.SimStorageService;
import com.pomr.service.synthesize.ManufacturerService;
import com.pomr.util.common.CommonData;
import com.pomr.util.common.JWT;
import com.pomr.util.common.ResponseData;
import com.pomr.util.page.Page;
import com.pomr.util.page.PageParam;

/**
 * SIM卡库存
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/simStorage")
public class SimStorageController {
	@Autowired
	@Qualifier("simStorageServiceImpl")
	private SimStorageService simStorageService;
	@Autowired
	@Qualifier("manufacturerServiceImpl")
	private ManufacturerService manufacturerService;
	
	/**
	 * 查询库存信息  （运营商、机构、设备）
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-search",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<SimStorage>> searchSimStorages(PageParam param){
		try{
			String page = param.getPage();
			String limit = param.getLimit();
			Map<String, String> map = param.getParams();
			String networkid = map.get("networkid");
			String agencyid = map.get("agencyid");
			String manufacturerid = map.get("manufacturerid");
			return simStorageService.searchStorages(networkid, agencyid, manufacturerid, page, limit);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 通过主表id查询库存详细信息 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inter-searchdetail",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<SimStorageDetail>> searchStorages(PageParam param) throws Exception{
			String id = param.getParams().get("id");
			String page = param.getPage();
			String limit = param.getLimit();
			return simStorageService.searchStorageDetails(id, page, limit);
	}
	
	/**
	 * 删除SIM卡信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/inter-delete",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteSimStorages(@RequestBody Map<String,String> map){
		ResponseData pd = null;
		try {
			String ids = map.get("id");
			if(ids==null || ids.equals("")){
				pd = ResponseData.custom("参数错误，未传递主键");
			}else{
				if(simStorageService.deleteStorageDetails(ids.split(","))){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.custom("删除失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		return pd;
	}
	
	/**
	 * SIM卡报损
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/inter-damage",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData damageSimStorages(@RequestBody Map<String,String> map){
		ResponseData pd = null;
		try {
			String ids = map.get("id");
			if(ids==null || ids.equals("")){
				pd = ResponseData.custom("参数错误，未传递主键");
			}else{
				if(simStorageService.damageStorageDetails(ids.split(","))){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.custom("报损失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		return pd;
	}
	
	/**
	 * SIM卡修复
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/inter-repair",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData repairSimStorages(@RequestBody Map<String,String> map){
		ResponseData pd = null;
		try {
			String ids = map.get("id");
			if(ids==null || ids.equals("")){
				pd = ResponseData.custom("参数错误，未传递主键");
			}else{
				if(simStorageService.repairStorageDetails(ids.split(","))){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.custom("报损失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		return pd;
	}
	
	/**
	 * 更新领用定金
	 * @param storage
	 * @return
	 */
	@RequestMapping(value="/inter-deitmoney",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData editEarnestmoney(@RequestBody SimStorage storage){
		ResponseData pd = null;
		try {
			if(storage==null || storage.getId()==null){
				pd = ResponseData.custom("参数错误，未传递主键");
			}else{
				if(simStorageService.editMoney(storage)){
					pd = ResponseData.ok();
				}else{
					pd = ResponseData.custom("更新失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			pd = ResponseData.custom(e.getMessage());
		}
		return pd;
	}
	
	/**
	 * 清库
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/inter-clean",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData cleanSimStorage(@RequestBody Map<String,String> map) throws Exception{
		ResponseData pd = null;
		String ids = map.get("id");
		if(ids==null || ids.equals("")){
			pd = ResponseData.custom("参数错误，未传递主键");
		}else{
			String reason = map.get("reason");
			if(simStorageService.cleanStorageDetails(ids.split(","),reason)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.custom("清库失败");
			}
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-searchbind",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData searchBindEquipment(@RequestBody Map<String,String> map) throws Exception{
		ResponseData pd = null;
		String id = map.get("id");
		if(id==null || id.equals("")){
			pd = ResponseData.custom("参数错误，未传递主键");
		}else{
			EquipmentStorageDetail detail = simStorageService.searchBindEquipment(id);
			if(detail == null){//没有绑定信息
				pd = ResponseData.ok();
				//获取设备类型
				List<EquipmentKind> kinds = (List<EquipmentKind>) CommonData.data.get("EquipmentKind");
				pd.putDataValue("kinds", kinds);
				//获取厂商信息
				List<Manufacturer> manufacturers = manufacturerService.searchManufacturersOfAgency(null);
				pd.putDataValue("manufacturers", manufacturers);
			}else{
				pd = ResponseData.ok();
				pd.putDataValue("detail", detail);
			}
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-searchequip",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData searchEquipmentLd(@RequestBody Map<String,String> map, HttpServletRequest request) throws Exception{
		ResponseData pd = null;
		String kindid = map.get("kind");
		String manufacturerid = map.get("manufacturer");
		String spec = map.get("spec");
		User user  = JWT.findUserByToken(request);
		List<EquipmentStorageDetail> details = simStorageService.searchEquipmentLd(kindid, manufacturerid, spec,user);
		pd = ResponseData.ok();
		pd.putDataValue("details", details);
		return pd;
	}
	
	@RequestMapping(value="/inter-searchSimOfNoUse",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<SimStorageDetail>> searchSimBySnOfNoUse(PageParam param, HttpServletRequest request) throws Exception{
		User user = JWT.findUserByToken(request);
		return simStorageService.searchSimBySnOfNoUse(param.getPage(),param.getLimit(), param.getParams().get("conds"),user);
	}

}
